package com.imooc.security.browser;

import com.imooc.security.core.authentication.FormAuthenticationConfig;
import com.imooc.security.core.authentication.mobile.SmsCodeAuthenticationSecurityConfig;
import com.imooc.security.core.authorize.AuthorizeConfigManager;
import com.imooc.security.core.properties.SecurityProperties;

import com.imooc.security.core.vaildate.code.ValidateCodeFilter;
import com.imooc.security.core.vaildate.code.ValidateCodeSecurityConfig;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.social.security.SocialAuthenticationProvider;
import org.springframework.social.security.SpringSocialConfigurer;
import org.springframework.social.security.provider.SocialAuthenticationService;


import javax.sql.DataSource;
import java.util.Set;

/**
 * @author liaofuxing
 * @date 2019/03/11 21:09
 * @E-mail liaofuxing@outlook.com
 */
@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;


    @Autowired
    private AuthorizeConfigManager authorizeConfigManager;

    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;

    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Autowired
    private FormAuthenticationConfig formAuthenticationConfig;


    @Autowired
    private DataSource dataSource;


    @Autowired
    private Set<BrowserSecurityConfigCallback> configCallbacks;

    @Autowired
    private SpringSocialConfigurer imoocSpringSocialConfigurer;


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        //tokenRepository.setCreateTableOnStartup(true);
        return tokenRepository;
    }




    @Override
    protected void configure(HttpSecurity http) throws Exception{
        if(CollectionUtils.isNotEmpty(configCallbacks)) {
            configCallbacks.forEach(callback -> callback.config(http));
        }
        //form表单提交
        formAuthenticationConfig.configure(http);
        http
            .apply(validateCodeSecurityConfig)
                .and()
            .apply(smsCodeAuthenticationSecurityConfig)
                .and()
            .apply(imoocSpringSocialConfigurer)
                .and()
                .authorizeRequests()
                .antMatchers("/authentication/require",
                        securityProperties.getBrowser().getLoginPage(),
                        securityProperties.getBrowser().getSignUpUrl(),
                        "/code/*",
                        "/user/regist").permitAll()
                .anyRequest()
                .authenticated()
                .and().csrf().disable();

        authorizeConfigManager.config(http.authorizeRequests());
    }

}
